let express=require('express')
let router=express.Router()
let crypto=require('crypto')
let sqlQuery=require('../lcSql')
let jiami=require('../md5')

router.get('/',(req,res)=>{
    res.render('register')
})

router.post('/',async function(req,res){
    console.log(req.body);//{'username':  ,'password':  ,'yzm':}
    //验证是否已有该用户
    let username=req.body.username
    let password=req.body.password
    let sqlStr="select username from users where username= ?"
    let result=await sqlQuery(sqlStr,[username])

    //该用户已经注册
    if(result.length>0){
        res.render('info',{
            title:"注册失败",
            content:"此用户已注册，可直接登陆，或找寻密码",
            href:"/register",
            hrefTxt:"注册页"
        })
    }else{//未注册
        let sqlStr="insert into users (username,password) values (?,?)";
        await sqlQuery(sqlStr,[username,jiami.md5(password+jiami.salt)])
        res.render('info',{
            title:"注册成功",
            content:"注册成功，即将进入登陆页面",
            href:"/login",
            hrefTxt:"登录页"
        })
    }

})



module.exports=router